const Util = {
	GetCurrentTime: function() {
		var date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		month = month < 10 ? ('0' + month) : month;
		let day = date.getDate();
		day = day < 10 ? ('0' + day) : day;
		let hour = date.getHours();
		hour = hour < 10 ? ('0' + hour) : hour
		let min = date.getMinutes();
		min = min < 10 ? ('0' + min) : min;
		let seconds = date.getSeconds();
		seconds = seconds < 10 ? ('0' + seconds) : seconds;
		var datetime = `${year}-${month}-${day} ${hour}:${min}:${seconds}`;
		console.log(datetime);
		return datetime;

	},
	GetCurrentDate: function() {
		var date = new Date();
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		month = month < 10 ? ('0' + month) : month;
		let day = date.getDate();
		day = day < 10 ? ('0' + day) : day;
		var datetime = `${year}-${month}-${day}`;
		return datetime;

	},
	isValidIp: function(e) {
		return /^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/.test(e)
	},
	//验证端口
	isValidPort: function(e) {
		var exp = /^(\d)+$/g;
		return exp.test(e) && parseInt(e) <= 65535 && parseInt(e) >= 0
	},
	//ASCII码转16进制
	strToHexCharCode: function(str) {
		if (str === "") {
			return "";
		} else {
			var hexCharCode = [];
			for (var i = 0; i < str.length; i++) {
				hexCharCode.push((str.charCodeAt(i)).toString(16));
			}
			return hexCharCode.join("");
		}
	},
	//十六进制转ASCII码
	hexCharCodeToStr: function(hexCharCodeStr) {
		var trimedStr = hexCharCodeStr.trim();
		var len = trimedStr.length;
		if (len % 2 !== 0) {
			console.log("存在非法字符!");
			return "";
		}
		var curCharCode;
		var resultStr = [];
		for (var i = 0; i < len; i = i + 2) {
			curCharCode = parseInt(trimedStr.substr(i, 2), 16);
			resultStr.push(String.fromCharCode(curCharCode));
		}
		return resultStr.join("");
	},

	utf8ByteToUnicodeStr: function(utf8Bytes) {
		var unicodeStr = "";
		for (var pos = 0; pos < utf8Bytes.length;) {
			var flag = utf8Bytes[pos];
			var unicode = 0;
			if ((flag >>> 7) === 0) {
				unicodeStr += String.fromCharCode(utf8Bytes[pos]);
				pos += 1;

			} else if ((flag & 0xFC) === 0xFC) {
				unicode = (utf8Bytes[pos] & 0x3) << 30;
				unicode |= (utf8Bytes[pos + 1] & 0x3F) << 24;
				unicode |= (utf8Bytes[pos + 2] & 0x3F) << 18;
				unicode |= (utf8Bytes[pos + 3] & 0x3F) << 12;
				unicode |= (utf8Bytes[pos + 4] & 0x3F) << 6;
				unicode |= (utf8Bytes[pos + 5] & 0x3F);
				unicodeStr += String.fromCodePoint(unicode);
				pos += 6;

			} else if ((flag & 0xF8) === 0xF8) {
				unicode = (utf8Bytes[pos] & 0x7) << 24;
				unicode |= (utf8Bytes[pos + 1] & 0x3F) << 18;
				unicode |= (utf8Bytes[pos + 2] & 0x3F) << 12;
				unicode |= (utf8Bytes[pos + 3] & 0x3F) << 6;
				unicode |= (utf8Bytes[pos + 4] & 0x3F);
				unicodeStr += String.fromCodePoint(unicode);
				pos += 5;

			} else if ((flag & 0xF0) === 0xF0) {
				unicode = (utf8Bytes[pos] & 0xF) << 18;
				unicode |= (utf8Bytes[pos + 1] & 0x3F) << 12;
				unicode |= (utf8Bytes[pos + 2] & 0x3F) << 6;
				unicode |= (utf8Bytes[pos + 3] & 0x3F);
				unicodeStr += String.fromCodePoint(unicode);
				pos += 4;

			} else if ((flag & 0xE0) === 0xE0) {
				unicode = (utf8Bytes[pos] & 0x1F) << 12;;
				unicode |= (utf8Bytes[pos + 1] & 0x3F) << 6;
				unicode |= (utf8Bytes[pos + 2] & 0x3F);
				unicodeStr += String.fromCharCode(unicode);
				pos += 3;

			} else if ((flag & 0xC0) === 0xC0) { //110
				unicode = (utf8Bytes[pos] & 0x3F) << 6;
				unicode |= (utf8Bytes[pos + 1] & 0x3F);
				unicodeStr += String.fromCharCode(unicode);
				pos += 2;

			} else {
				unicodeStr += String.fromCharCode(utf8Bytes[pos]);
				pos += 1;
			}
		}
		return unicodeStr;
	}


}


export default Util